﻿#include "role.h"

Role::Role(QObject *parent): QBaseEntity(parent)
{
    clear();
    const QMetaObject *metaObject = this->metaObject();
    setTableName(QString(metaObject->className()).toLower());
    setPrimaryKey("id");
    setUniqueKeys(QStringList() << "code");
    setNotNullKeys(QStringList() << "name"<<"code");
}

Role::Role(uint64_t id, QString name,QString code, QStringList permissions, QString remark, int status)
{
    clear();
    const QMetaObject *metaObject = this->metaObject();
    setTableName(QString(metaObject->className()).toLower());
    setPrimaryKey("id");
    setUniqueKeys(QStringList() << "code");
    setNotNullKeys(QStringList() << "name"<<"code");
    this->id = id;
    this->name = name;
    this->code = code;
    this->permissions = permissions;
    this->remark = remark;
    this->status = status;
}

Role::~Role()
{

}

QJsonObject Role::toJsonObject(QBaseEntity::PropertiesType type, QStringList propertyList) const
{
    QVariantMap properties;
    switch (type) {
    case UnEmptyProperty:
        properties = getUnEmptyProperties();
        break;
    case CustomProperty:
        properties = getCustomProperties(propertyList);
        break;
    case IgnoreOutProperty:
        properties = getIgnoreOutProperties(propertyList);
        break;
    default:
        properties = getAllProperties();
        break;
    }

    QJsonObject jsonObject;
    for (auto it = properties.begin(); it != properties.end(); ++it) {
        if(it.key() == "permissions"){
            QList<QVariant> list = it.value().toList();
            QJsonArray permissions;
            for(QVariant variant:list){
                permissions.append(QJsonValue::fromVariant(variant.toULongLong()));
            }
            jsonObject[it.key()] = permissions;
        }else{
           jsonObject[it.key()] = qvariantToJsonValue(it.value());
        }
    }
    return jsonObject;
}

uint64_t Role::getId() const
{
    return id;
}

void Role::setId(const uint64_t &value)
{
    id = value;
}

QString Role::getName() const
{
    return name;
}

void Role::setName(const QString &value)
{
    name = value;
}

QStringList Role::getPermissions() const
{
    return permissions;
}

void Role::setPermissions(const QStringList &value)
{
    permissions = value;
}

QString Role::getRemark() const
{
    return remark;
}

void Role::setRemark(const QString &value)
{
    remark = value;
}

int Role::getStatus() const
{
    return status;
}

void Role::setStatus(int value)
{
    status = value;
}

QString Role::getCode() const
{
    return code;
}

void Role::setCode(const QString &value)
{
    code = value;
}

